package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbCourseRecommend;
import com.beetlestech.nfe.main.biz.com.dto.CourseInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.CourseListDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.view.VcourserecommendDTO;


public class HbCourseRecommendImpl extends AbstractDBLogic implements HbCourseRecommend, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "推荐课程信息表";

	public CourseListDTO getCourseList(Integer rowStar, Integer rowEnd, String starDate, Integer sort){
		CourseListDTO courseRecommend = new CourseListDTO();
		List<CourseInfoDTO> courseList = new ArrayList<CourseInfoDTO>();
		
		StatementDTO statementDTO = new StatementDTO();

		if(starDate != null){
			StatementUtil.andWhere(statementDTO, VcourserecommendDTO.COLUMN_COURSESTART, StatementUtil.KEYWORD.GREATER_EQUAL_THAN, starDate);
		}
		
		VcourserecommendDTO searchDTO = new VcourserecommendDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		if(sort!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(VcourserecommendDTO.COLUMN_COURSESTART);
			if(sort == 0){
				sb.append(" ASC");
			}
			else{
				sb.append(" DESC");
			}
			searchDTO.set_orderByCondition(sb.toString());
		}
		
		courseRecommend.setRecordCount(queryDAO.count(VcourserecommendDTO.COUNT_SEARCH
				, searchDTO));
		
		StatementUtil.andWhere(statementDTO, "rowNo", StatementUtil.KEYWORD.GREATER_EQUAL_THAN, rowStar);
		StatementUtil.andWhere(statementDTO, "rowNo", StatementUtil.KEYWORD.LESS_EQUAL_THAN, rowEnd);
		
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		
		List<VcourserecommendDTO> courseInfos = queryDAO.queryForObjectList(SQLID.VCOURSERECOMMEND_PAGE_SQL_SEARCH, searchDTO);
		
		for(VcourserecommendDTO item : courseInfos){
			courseList.add(convertToCourseInfo(item));
		}
		
		courseRecommend.setCourseInfoArray(courseList.toArray(new CourseInfoDTO[courseList.size()]));
		return courseRecommend;
	}

	private CourseInfoDTO convertToCourseInfo(VcourserecommendDTO item) {
		CourseInfoDTO courseInfo = new CourseInfoDTO();
		courseInfo.setCourseId(item.getcourseid());
		courseInfo.setCourseStart(item.getcoursestart());
		courseInfo.setName(item.getname());
		courseInfo.setAnnotate(item.getannotate());
		courseInfo.setPath(item.getpath());
		courseInfo.setPrice(item.getprice());
		courseInfo.setDiscount(Math.ceil(item.getdiscount() * item.getprice() * 10)/10);
		courseInfo.setCoursePeriod(item.getcourseperiod());
		courseInfo.setCourseEnd(item.getcourseend());
		return courseInfo;
	}
}
